package tree;

public class BinaryTreeDemo {    //二叉树

    public static void main(String[] args) {
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node1 = new HeroNode(2, "吴用");
        HeroNode node2 = new HeroNode(3, "卢俊义");
        HeroNode node3 = new HeroNode(4, "林冲");
        HeroNode node4 = new HeroNode(5, "关胜");


        //手动创建二叉树
        root.setLeft(node1);
        root.setRight(node2);

        node2.setLeft(node4);
        node2.setRight(node3);

        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(root);
        //前序遍历
        System.out.println("前序遍历");
        binaryTree.perOrder();
        System.out.println("中序遍历");
        binaryTree.infixOrder();
        System.out.println("后序遍历");
        binaryTree.postOrder();

        System.out.println("======");
        HeroNode heroNode1 = binaryTree.preOrderSearch(5);
        System.out.println(heroNode1);

        HeroNode heroNode2 = binaryTree.infixOrderSearch(5);
        System.out.println(heroNode2);

        HeroNode heroNode3 = binaryTree.postOrderSearch(5);
        System.out.println(heroNode3);
        //删除
        binaryTree.delNode(5);
        System.out.println("==========删除节点=========");
        binaryTree.perOrder();
    }


}

//定义二叉树
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void perOrder() {//前序遍历
        if (this.root == null) {
            System.out.println("当前二叉树为空不能遍历");
        } else {
            this.root.preOrder();
        }
    }

    public void infixOrder() {//中序遍历
        if (this.root == null) {
            System.out.println("当前二叉树为空不能遍历");
        } else {
            this.root.infixOrder();
        }
    }

    public void postOrder() {//后序遍历
        if (this.root == null) {
            System.out.println("当前二叉树为空不能遍历");
        } else {
            this.root.postOrder();
        }
    }

    public HeroNode preOrderSearch(int No) {
        if (this.root == null) {
            System.out.println("当前二叉树为空不能遍历");
        }
        return this.root.preOrderSearch(No);

    }

    public HeroNode postOrderSearch(int No) {
        if (this.root == null) {
            System.out.println("当前二叉树为空不能遍历");
        }
        return this.root.postOrderSearch(No);

    }

    public HeroNode infixOrderSearch(int No) {
        if (this.root == null) {
            System.out.println("当前二叉树为空不能遍历");
        }
        return this.root.infixOrderSearch(No);

    }

    public void delNode(int no) {
        if (this.root == null) {
            System.out.println("当前二叉树为空不能遍历");
        } else if (root.getNo() == no) {
            root = null;
            return;
        } else {
            root.delNode(no);
        }
    }

}


class HeroNode {
    private int no;
    private String name;

    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" + "no=" + no + ", name='" + name + '}';
    }

    //前序遍历  父左右
    public void preOrder() {
        System.out.println(this); //先输出父节点
        if (this.left != null) { //向左递归
            this.left.preOrder();
        }
        if (this.right != null) { //向右递归
            this.right.preOrder();
        }
    }

    //中心遍历  左父右
    public void infixOrder() {
        if (this.left != null) { //向左递归
            this.left.infixOrder();
        }
        System.out.println(this); //输出父节点

        if (this.right != null) { //向右递归
            this.right.infixOrder();
        }
    }

    //后续遍历  左右父
    public void postOrder() {
        if (this.left != null) { //向左递归
            this.left.postOrder();
        }
        if (this.right != null) { //向右递归
            this.right.postOrder();
        }
        System.out.println(this); //输出父节点
    }

    //前序查找
    public HeroNode preOrderSearch(int No) {

        HeroNode temp = null; //用于接收查找到的值
        if (this.left != null) {
            temp = this.left.preOrderSearch(No);
        }
        if (this.no == No) {
            return this;
        }
        if (temp != null) {
            return temp;
        }
        if (this.right != null) {
            temp = this.right.preOrderSearch(No);
        }
        return temp;
    }

    //中序查找
    public HeroNode infixOrderSearch(int No) {
        HeroNode temp = null; //用于接收查找到的值
        if (this.left != null) {
            temp = this.left.infixOrderSearch(No);
        }
        if (temp != null) {
            return temp;
        }
        if (this.no == No) {
            return this;
        }
        if (this.right != null) {
            temp = this.right.infixOrderSearch(No);
        }
        return temp;
    }


    //后序查找
    public HeroNode postOrderSearch(int No) {
        HeroNode temp = null; //用于接收查找到的值
        if (this.left != null) {
            temp = this.left.postOrderSearch(No);
        }
        if (temp != null) {
            return temp;
        }

        if (this.right != null) {
            temp = this.right.postOrderSearch(No);
        }
        if (this.no == No) {
            return this;
        }
        return temp;
    }

    //删除
    public void delNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        //向左递归 删除
        if (this.left != null) {
            this.left.delNode(no);
        }
        //向右递归 删除
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

}